package com.practicesuite.controllayer;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import com.practicesuite.systemlayer.Appointment;
import com.practicesuite.systemlayer.Doctor;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.util.GregorianCalendar;

/**
 * Panel for viewing details for a specific appointment.
 * 
 * @author James
 *
 */
public class ViewAppointmentPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    GregorianCalendar date = new GregorianCalendar();
    Doctor doctor;

    /**
     * Create the panel.
     */
    public ViewAppointmentPanel(MainWindow container, final Appointment appointment, final User user) {
        this.window = container;
        setLayout(new GridLayout(0, 1, 0, 0));
        setPreferredSize(new Dimension(500, 400));
        
        JLabel lblAppointment = new JLabel("Appointment: " + appointment.getAppDate());
        lblAppointment.setHorizontalAlignment(SwingConstants.CENTER);
        lblAppointment.setFont(new Font("Tahoma", Font.PLAIN, 24));
        add(lblAppointment);
        
        JPanel panel = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        add(panel);
        
        JLabel lblTime = new JLabel(" Time: ");
        lblTime.setFont(new Font("Tahoma", Font.BOLD, 12));
        panel.add(lblTime);
        
        JLabel lblTimeData = new JLabel(appointment.getAppointmentTime());
        lblTimeData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(lblTimeData);
        
        JPanel panel_1 = new JPanel();
        FlowLayout flowLayout_3 = (FlowLayout) panel_1.getLayout();
        flowLayout_3.setAlignment(FlowLayout.LEFT);
        add(panel_1);
        
        JLabel lblReasonForAppointment = new JLabel(" Reason for appointment: ");
        lblReasonForAppointment.setFont(new Font("Tahoma", Font.BOLD, 12));
        panel_1.add(lblReasonForAppointment);
        
        JLabel lblReasonData = new JLabel(appointment.getReasonForApp());
        lblReasonData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_1.add(lblReasonData);
        
        JPanel panel_2 = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) panel_2.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        add(panel_2);
        
        JLabel lblAdditionalNotes = new JLabel(" Patient: ");
        lblAdditionalNotes.setFont(new Font("Tahoma", Font.BOLD, 12));
        panel_2.add(lblAdditionalNotes);
        
        JLabel lblNotesData = new JLabel(appointment.getPatient().getName() + 
                " (ID: " + Integer.toString(appointment.getPatient().getUserID()) + ")");
        lblNotesData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_2.add(lblNotesData);
        
        JPanel panel_3 = new JPanel();
        FlowLayout flowLayout_2 = (FlowLayout) panel_3.getLayout();
        flowLayout_2.setAlignment(FlowLayout.LEFT);
        add(panel_3);
        
        JLabel lblDesiredDoctor = new JLabel(" Desired doctor: ");
        lblDesiredDoctor.setFont(new Font("Tahoma", Font.BOLD, 12));
        panel_3.add(lblDesiredDoctor);
        
        try {
            doctor = SystemDriver.getDoctorById(appointment.getDesiredDoctorID());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
        }
        JLabel lblDoctorData = new JLabel(doctor.getName());
        lblDoctorData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_3.add(lblDoctorData);
        
        JPanel panel_4 = new JPanel();
        add(panel_4);

        JButton btnCancelAppointment = new JButton("Cancel Appointment");
        btnCancelAppointment.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                SystemDriver.deleteAppointnment(appointment);
                JOptionPane.showMessageDialog(null, "Appointment canceled.");
                int eUID = user.getEUID();
                if (eUID == 0) {
                    window.gotoPanel("adminHome", user);
                }
                else if (eUID == 1) {
                    window.gotoPanel("doctorHome", user);
                }
                else if (eUID == 2) {
                    window.gotoPanel("nurseHome", user);
                }
                else if (eUID == 3) {
                    window.gotoPanel("patientHome", user);
                }
            }
        });
        btnCancelAppointment.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_4.add(btnCancelAppointment);
        
        JButton btnReturnToHome = new JButton("Return to Home");
        btnReturnToHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int eUID = user.getEUID();
                if (eUID == 0) {
                    window.gotoPanel("adminHome", user);
                }
                else if (eUID == 1) {
                    window.gotoPanel("doctorHome", user);
                }
                else if (eUID == 2) {
                    window.gotoPanel("nurseHome", user);
                }
                else if (eUID == 3) {
                    window.gotoPanel("patientHome", user);
                }
            }
        });
        btnReturnToHome.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_4.add(btnReturnToHome);
        
        JPanel panel_5 = new JPanel();
        add(panel_5);
        
        JPanel panel_6 = new JPanel();
        add(panel_6);
    }

}
